{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# YOLOv11 训练 Notebook",
   "id": "2392cc9be5edf3fa"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 1. 环境准备 & 导入包\n",
    "按照 README.md 文档中，将环境准备好之后，在训练代码中导入包"
   ],
   "id": "7f50d489355a6486"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "from ultralytics import YOLO                    # YOLOv11 模型接口\n",
    "import yaml                                     # 用于读取 YAML 配置文件\n",
    "from IPython.display import Image, display      # 用于显示图片\n",
    "import os                                       # 文件与路径操作\n",
    "\n",
    "print('环境准备完成：依赖包已导入')"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 2. 加载模型 & 权重\n",
    "加载 YOLOv11 模型配置，并（可选）加载预训练权重"
   ],
   "id": "58391b3de256e74b"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "model_cfg = 'ultralytics/cfg/models/yolo11s.yaml'  # 模型配置文件路径\n",
    "pretrained_weights = 'datasets/weeds/weeds.pt'     # 预训练权重路径（可选）\n",
    "\n",
    "# 加载模型配置\n",
    "model = YOLO(model_cfg)\n",
    "print(f'已加载模型配置：{model_cfg}')\n",
    "\n",
    "# 如果预训练权重存在，则加载它\n",
    "if os.path.exists(pretrained_weights):\n",
    "    model.load(pretrained_weights)\n",
    "    print(f'已加载预训练权重：{pretrained_weights}')\n",
    "else:\n",
    "    print(f'未找到预训练权重，跳过加载：{pretrained_weights}')"
   ],
   "id": "bad4edf74f1bf0de",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "aa721cc6adac6926"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 3. 检查数据集配置\n",
    "打开并打印 `datasets/weeds.yaml`，确认 paths 正确"
   ],
   "id": "ef6c14d31f3681a1"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "data_cfg_path = 'datasets/weeds.yaml'  # 数据集配置文件\n",
    "# 读取 YAML\n",
    "with open(data_cfg_path, 'r', encoding='utf-8') as f:\n",
    "    data_cfg = yaml.safe_load(f)\n",
    "print(f'已读取数据集配置：{data_cfg_path}')\n",
    "print('配置内容：')\n",
    "print(data_cfg)\n",
    "\n",
    "# 验证关键字段存在\n",
    "assert 'train' in data_cfg and 'val' in data_cfg, '数据集配置缺少 train 或 val 路径'\n",
    "print('数据集配置检查通过')"
   ],
   "id": "f31982231af6260d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 4. 配置训练参数\n",
    "- 设置训练参数：图像尺寸、批量大小、Epoch 数量等"
   ],
   "id": "deaf1c5ae7b4b63f"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "train_params = {\n",
    "    'data': data_cfg_path,   # 数据配置路径\n",
    "    'imgsz': 640,            # 输入图像尺寸\n",
    "    'epochs': 20,            # 训练轮数\n",
    "    'batch': 8,              # 批大小\n",
    "    'workers': 4,            # 数据加载进程数\n",
    "    'device': 0,             # GPU id 或 'cpu'\n",
    "    'project': 'runs/train', # 保存结果的项目目录\n",
    "    'name': 'exp',           # 结果子目录名称\n",
    "}\n",
    "print('训练参数：')\n",
    "print(train_params)"
   ],
   "id": "ed3ccb7b0cfdc473",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 5. 启动训练\n",
    "- 训练过程需等待，图片越多，epochs 轮次越大，等待时间越长。"
   ],
   "id": "6fc6c7d2a4bb2fdd"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 启动训练\n",
    "print('开始训练...')\n",
    "model.train(**train_params)\n",
    "print('训练完成！')"
   ],
   "id": "995c207af1f2d6d0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 6. 可视化训练结果\n",
    "- 训练结束后，在 `runs/train/exp` 目录下生成 `results.png`\n",
    "- 首次训练保存路径：`runs/train/exp`\n",
    "- 请查看`5. 启动训练`输出的实际路劲，如：Results saved to runs\\train\\exp\n",
    "- 使用 matplotlib 显示训练曲线"
   ],
   "id": "e5e61d671119b94a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 你的训练曲线文件路径\n",
    "img_path = 'runs/train/exp/results.png'\n",
    "\n",
    "if os.path.exists(img_path):\n",
    "    print(f'已找到文件，正在显示：{img_path}')\n",
    "    display(Image(img_path))\n",
    "else:\n",
    "    print(f'提示：未找到文件，请检查路径是否正确：{img_path}')"
   ],
   "id": "2bce59185026431b",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
